package org.webequa.core.maple.quantity;

import org.apache.log4j.Logger;
import org.webequa.core.common.quantity.ComplexNumber;
import org.webequa.core.generic.signature.GenericQuantityObjectSignature;

import com.maplesoft.externalcall.MapleException;
import com.maplesoft.openmaple.Algebraic;

public class MapleComplexNumber extends MapleQuantityObject implements
		ComplexNumber
{
	public MapleComplexNumber()
	{
		super();
		signature = GenericQuantityObjectSignature.getComplexNumberSignature();
		try
		{
			mapleObject = mapleEngine.newName(mapleVarName, true);
			mapleObject.assign(mapleEngine.newNumeric(0.0));
		} catch (MapleException e)
		{
			log.error(e);
			mapleObject = null;
		}
	}
	public MapleComplexNumber(double real, double image)
	{
		super();
		signature = GenericQuantityObjectSignature.getComplexNumberSignature();
		try
		{
			mapleObject = mapleEngine.newName(mapleVarName, true);
			String mapleJob = "Complex("+real+","+image+"):";
			mapleObject.assign(mapleEngine.evaluate(mapleJob));
			log.info(mapleJob);
		} catch (MapleException e)
		{
			log.error(e);
			mapleObject = null;
		}		
	}
	
	@Override
	public Double getArg()
	{
		String mapleJob = "argument("+mapleVarName+"):";
		try
		{
			Algebraic res = mapleEngine.evaluate(mapleJob);
			if (res!=null)
				return res.evalhf();
			else return null;
		} catch (MapleException e)
		{
			log.error(e);
			return null;
		}
	}

	@Override
	public Double getImagePart()
	{
		String mapleJob = "Im("+mapleVarName+"):";
		try
		{
			Algebraic res = mapleEngine.evaluate(mapleJob);
			if (res!=null)
				return res.evalhf();
			else return null;
		} catch (MapleException e)
		{
			log.error(e);
			return null;
		}
	}

	@Override
	public Double getMod()
	{
		String mapleJob = "abs("+mapleVarName+"):";
		try
		{
			Algebraic res = mapleEngine.evaluate(mapleJob);
			if (res!=null)
				return res.evalhf();
			else return null;
		} catch (MapleException e)
		{
			log.error(e);
			return null;
		}
	}

	@Override
	public Double getRealPart()
	{
		String mapleJob = "Re("+mapleVarName+"):";
		try
		{
			Algebraic res = mapleEngine.evaluate(mapleJob);
			if (res!=null)
				return res.evalhf();
			else return null;
		} catch (MapleException e)
		{
			log.error(e);
			return null;
		}
	}

	@Override
	public Double getValue()
	{
		return getMod();
	}
	private static final Logger log = Logger.getLogger(MapleComplexNumber.class);
	@Override
	public void setValue(double arg0)
	{
		// TODO Auto-generated method stub
		
	}
	@Override
	public void setRealPart(double arg0)
	{
		// TODO Auto-generated method stub
		
	}
	@Override
	public void setImagePart(double arg0)
	{
		// TODO Auto-generated method stub
		
	}
	@Override
	public void setArg(double arg0)
	{
		// TODO Auto-generated method stub
		
	}
	@Override
	public void setMod(double arg0)
	{
		// TODO Auto-generated method stub
		
	}
}
